import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Random;

public class PLExquisiteCorpse {
	String[] mPlayerNames = null;
	PLPlayers mPlayers = new PLPlayers();
	volatile boolean mExit = false;


	public PLExquisiteCorpse() {}

	private boolean InitSketch() {
		PLSketch.Obj().clear();
		return true;
	}


	private void GetPlayerNames() {
		// TODO: this function will get the UI up to chooses the player names.
			mPlayerNames = new String[2]; 
			mPlayerNames[0] = "UI";
			mPlayerNames[1] = "Nick";		
	}

	private boolean InitPlayers() {
		GetPlayerNames();
		if (0 == mPlayerNames.length) {
			PLDebug.Err("No Player Names");
			return false;
		}

		for (int i =0; i < mPlayerNames.length; ++i) {
			PLPlayer player = PLPlayer.GetPlayerFromName(mPlayerNames[i]);
			if (player != null)
				mPlayers.AddPlayer(player);
			else 
				PLDebug.Warn("Player %s not init", mPlayerNames[i]);
		}
		return true;	
	}
	
	private boolean Init() {
		if(!InitSketch()) {
			PLDebug.Warn("Could Not init sketch");
			return false;
		}

		if(!InitPlayers()) {
			PLDebug.Warn("Could Not init Players");
			return false;
		}

		return true;			
	}

	private PLShape GenFirstShape() {

		Random generator = new Random();

		// starting point		
		Point sPoint =  new Point(generator.nextInt(PLGuiInfo.Obj().GetWidth()/2), generator.nextInt(PLGuiInfo.Obj().GetHeight()/2));		

		Rectangle sQuad = PLShapeUtils.GetQuadrantRect(generator.nextInt(4));
		sPoint.x += (int)sQuad.getX();
		sPoint.y += (int)sQuad.getY();		

		Point ePoint =  new Point(generator.nextInt(PLGuiInfo.Obj().GetWidth()/2), generator.nextInt(PLGuiInfo.Obj().GetHeight()/2));		
		Rectangle eQuad = PLShapeUtils.GetQuadrantRect(generator.nextInt(4));
		ePoint.x += (int)sQuad.getX();
		ePoint.y += (int)sQuad.getY();
		PLShape shape = new PLLine(sPoint, ePoint);
		PLSketch.Obj().DrawShape(shape, 0, new Color(0, 0, 0));
		return shape;
	}

	private boolean PlayGame() {	
		mPlayers.BeginGet();
		PLShape query = GenFirstShape();
		while(!mExit && mPlayers.NumPlayers() > 0) {
			PLPlayer player = mPlayers.GetNextPlayer();
			if (null == player) {
				mPlayers.BeginGet();
				continue;
			}
			
			PLOutShape response = new PLOutShape();			
			float confidence = player.GenResponse(query, response);
			if (confidence > 0.3) {
				PLSketch.Obj().DrawShape(response.GetShape(), mPlayers.GetLastPlayerIndex(), player.GetColor());
				query = response.GetShape();
			} else {
				PLDebug.Warn("Player Skipped Turn");
			}
		}
		return true;
	}

	private boolean Shutdown() {
		return true;
	}

	public void Play() {
		if(!Init()) {
			PLDebug.Err("Could Not init game");
			return;
		}
			
		PlayGame();
		Shutdown();
	}

	public void ExitNow() {
		mExit = true;
	}	

	/*
	public static void main(String[] args) {
		PLExquisiteCorpse game = new PLExquisiteCorpse();

		// in a different thread
		game.Play();
		// add an exit button in a different thread
		
	}
	*/
}
